﻿namespace CCF20Module.DeviceState
{
    using CCF20Model;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using WiseLink.RDS.Resource;

    public class SellToCustomerState : DeviceStateBase
    {

        public SellToCustomerState(IDeviceHandler handler, DeviceModel model,VehicleModel vModel, Device device, IList list)
            : base(handler, model,vModel, device, list)
        {
            this.failReason = DeviceStatus.Sold;
            StateName = DeviceStatus.Sold;
            ////LoggerHelper.LogCCF20("进入售出状态初始化");
        }

        #region ExecuteAction

        public override string runInStock()
        {
            if (currentDevice.IsRegistered == true || currentDevice.IsSpecialIDC == true)
            {
                return string.Format(Message.Device_CantInStock_Register, currentDevice.IDCSimNumber);
            }
            else
            {
                currentDevice.Customer.IDS = null;
                currentDevice.Customer.IDSID = null;
                currentDevice.Customer.SN = null;
                currentDevice.Customer.SNPwd = null;
                currentDevice.CustomerID = null;
                currentDevice.DeviceStatus = DeviceStatus.InStock;
                currentDevice.InStockDate = DateTime.Now;
                currentDevice.Customer = null;

                //// 归库时,我们是否要保存车辆的状态
                
                //currentDevice.Vehicle.PreviousContactResult = string.Empty;
                //currentDevice.Vehicle.LastContactResult = string.Empty;
                //currentDevice.Vehicle.LastContactTime = null;
                //currentDevice.Vehicle.LastServcieDate 
                
                
                currentDevice.Vehicle = null;
                currentDevice.IsDefected = false;
                currentDevice.UsageStatus = null;
                currentDevice.Sales = null;

                deviceModel.Update();
                
                this.deviceHandler.setState(deviceHandler.getInStockState());
                return string.Format(Message.Device_InStock_Succeed, currentDevice.IDCSimNumber);
            }
        }

        public override string runSellToCustomer(int customerId)
        {
            return base.runSellToCustomer(customerId);
        }
        public override string runRegisterIDC(string SN, string SNKey, string IDSName, string IDSID)
        {
            if (currentDevice.Vehicle == null)
            {
                this.failReason = Message.Device_CantRegister_NoVehicle;
                return base.runRegisterIDC();
            }
            else if (currentDevice.Customer == null)
            {
                this.failReason = Message.Device_CantRegister_NoCustomer;
                return base.runRegisterIDC();
            }
            
            if (eRDSZIC.DeviceRegister(currentDevice,IDSName, IDSID, SN, SNKey))
            {
                currentDevice.IsRegistered = true;
                //currentDevice.RegisterDate = registerDate;

                //currentDevice.Vehicle.LastContactResult = "未联系";

                deviceModel.AddDefaultMaintenanceToVehicle(currentDevice.Vehicle);
                deviceModel.Update();
                
                SMSModel.SendShortMessage(currentDevice.Vehicle, "开机", "开机通知模板");
                SMSModel.SendGPRSMessage(currentDevice.Vehicle, "开机", "开机通知模板");
                return string.Format(Message.Device_Register_Succeed, currentDevice.IDCSimNumber);
            }
            else
            {
                this.failReason = string.Empty;
                return base.runRegisterIDC();
            }

        }
        
        public override string runUnRegisterIDC()
        {
            if (currentDevice.IsSpecialIDC == true)
            {
                this.failReason = Message.Device_CantUnRegister_SpeicalIDC;
                return base.runUnRegisterIDC();
            }
            else
            {
                if (base.ShowConfirmationMessage(string.Format(Message.Device_UnRegsiter_Confirm, currentDevice.IDCSimNumber)))
                {
                    if (ZICBase.DeviceUnRegister(currentDevice))
                    {
                        currentDevice.IsRegistered = false;
                        currentDevice.RegisterDate = null;
                        
                        deviceModel.Update();
                        vehicleModel.VehicleRepairStart(currentDevice.Vehicle, DateTime.Now);
                        vehicleModel.Update();
                
                        return string.Format(Message.Device_UnRegister_Succeed, currentDevice.IDCSimNumber);
                    }
                    else
                    {
                        this.failReason = Message.ZIC_DataError;
                        return base.runUnRegisterIDC();
                    }
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public override string runRegisterSpeicalIDC()
        {
            if (currentDevice.IsRegistered == true)
            {
                if (base.ShowConfirmationMessage(string.Format(Message.Device_SpecialIDC_Confirm, currentDevice.IDCSimNumber)))
                {
                    if (ZICBase.SpecialIDCRegister(currentDevice))
                    {
                        currentDevice.IsSpecialIDC = true;
                        deviceModel.Update();
                        return string.Format(Message.Device_SpecialIDC_Succeed, currentDevice.IDCSimNumber);
                    }
                    else
                    {
                        this.failReason = Message.ZIC_DataError;
                        return base.runRegisterSpeicalIDC();
                    }
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                this.failReason = Message.Device_CantSpecialIDC_Register;
                return base.runRegisterSpeicalIDC();

            }
        }

        public override string runUnRegisterSpeicalIDC()
        {
            if (base.ShowConfirmationMessage(string.Format(Message.Device_UnSpecialIDC_Confirm, currentDevice.IDCSimNumber)))
            {

                if (ZICBase.SpecialIDCUnRegister(currentDevice))
                {
                    currentDevice.IsSpecialIDC = false;
                    deviceModel.Update();
                    return string.Format(Message.Device_UnSpecialIDC_Succeed, currentDevice.IDCSimNumber);
                }
                else
                {
                    this.failReason = Message.ZIC_DataError;
                    return base.runUnRegisterSpeicalIDC();
                }
            }
            else
            {
                return string.Empty;
            }
        }

        public override string runReturnDevice()
        {
            return base.runReturnDevice();
        }

        public override string runDeleteDevice()
        {
            return base.runDeleteDevice();
        }

        #endregion
    }
}
